package com.example.myapplication3.listView

import android.os.Bundle
import android.widget.AdapterView
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication3.Fruit
import com.example.myapplication3.listView.FruitAdapter
import com.example.myapplication3.R

class ListViewActivity : AppCompatActivity() {
    private val fruitList = ArrayList<Fruit>()
    lateinit var listView: ListView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list_view)
        listView = findViewById(R.id.listview)
        initFruits()        // 不初始化listView为空
        val adapter = FruitAdapter(this, R.layout.fruit_item, fruitList)
        listView.adapter = adapter

        // 新增对ListView的item点击事件处理
        listView.onItemClickListener = AdapterView.OnItemClickListener() { parent, view, position, id ->
            val fruit = fruitList[position]
            Toast.makeText(this, fruit.name, Toast.LENGTH_SHORT).show()

        }
    }

    private fun initFruits(){
        repeat(1){     // 重复添加次数
            fruitList.apply {
                add(Fruit("Apple", R.mipmap.ic_launcher))
                add(Fruit("Bpple", R.mipmap.ic_launcher))
                add(Fruit("Cpple", R.mipmap.ic_launcher))
                add(Fruit("Dpple", R.mipmap.ic_launcher))
                add(Fruit("Epple", R.drawable.ic_launcher_background))
                add(Fruit("Fpple", R.drawable.ic_launcher_background))
                add(Fruit("Gpple", R.drawable.ic_launcher_background))
                add(Fruit("Hpple", R.drawable.ic_launcher_background))
                add(Fruit("Ipple", R.drawable.ic_launcher_background))
                add(Fruit("Jpple", R.drawable.ic_launcher_background))
                add(Fruit("Kpple", R.drawable.ic_launcher_background))
                add(Fruit("Lpple", R.drawable.ic_launcher_background))
                add(Fruit("Mpple", R.drawable.ic_launcher_background))
                add(Fruit("Npple", R.drawable.ic_launcher_background))
                add(Fruit("Opple", R.drawable.ic_launcher_background))
                add(Fruit("Ppple", R.drawable.ic_launcher_background))
                add(Fruit("Qpple", R.drawable.ic_launcher_background))
                add(Fruit("Rpple", R.drawable.ic_launcher_background))
                add(Fruit("Spple", R.drawable.ic_launcher_background))
                add(Fruit("Tpple", R.drawable.ic_launcher_background))
                add(Fruit("Upple", R.drawable.ic_launcher_background))
                add(Fruit("Vpple", R.drawable.ic_launcher_background))
                add(Fruit("Wpple", R.drawable.ic_launcher_background))
                add(Fruit("Xpple", R.drawable.ic_launcher_background))
                add(Fruit("Ypple", R.drawable.ic_launcher_background))
                add(Fruit("Zpple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))
                add(Fruit("Apple", R.drawable.ic_launcher_background))

            }
        }
    }

   /* lateinit var listView: ListView
    val data = listOf("Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Strawberry", "Berry", "Mango",
        "Cat", "Dog", "Elephant", "Panda", "Monkey", "Cow", "Sheep", "Wolf","aaa",
        "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Strawberry", "Berry", "Mango",)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list_view)
        listView = findViewById(R.id.listview)
        val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data)
        listView.adapter = adapter
    }*/
}